

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Read implements Runnable {
   DataInputStream in;
   public void setDataInputStream(DataInputStream in) {
      this.in = in;
   }
   public void run() {
      double result=0;
       try {
           String answer=in.readUTF();
           String kkk=new String(answer);
           System.out.println(kkk);
           System.out.println("接收到的answer字符串为："+answer);
           System.out.println("下面对程序进行处理：");
           int sum = 0;
//           String value = "192.168.128.33";
           // 注意要加\\,要不出不来,yeah
           answer=answer.substring(12);
           System.out.println("截取完学号之后为："+answer);
           String[] names = answer.split("\\,");
           for (int i = 0; i < names.length; i++) {
               sum += Integer.parseInt(names[i]);
               System.out.println(names[i]);
           }
           System.out.println("sum=" + sum);
           double c = 0.0;
           c = (double) sum / names.length;
           System.out.println("c=" + c);
           
            //几个数
            for (int i = 0; i < names.length; i++) {
                //求和
                sum += Double.parseDouble(names[i]);
                System.out.println(names[i]);
            }
            System.out.println("总和：sum=" + sum);

            //平均值
            //double c = 0.0;
            c = (double) sum / names.length;
            System.out.println("平均值：c=" + c);

            //方差
            double d = 0.0;
            for (int j = 0; j < names.length; j++) {
                d += (Double.parseDouble(names[j]) - c) * (Double.parseDouble(names[j]) - c);
            }
            System.out.println("方差：d=" + d / names.length);
            System.out.println("标准差：为"+ Math.sqrt(d / names.length));

            
            //最大值 最小值
            //排序  从小到大
            String numa;
            for (int i = 0; i < names.length - 1; i++) {
               for (int j = 0; j < names.length - i - 1; j++) {
                   if (Double.parseDouble(names[j]) > Double.parseDouble(names[j + 1])) {
                       numa = names[j];
                       names[j] = names[j + 1];
                       names[j + 1] = numa;
                   }
               }
           }
            
            
           //
           System.out.println("");
           for(int i = 0;i < names.length;i ++){
               System.out.print(" "+names[i]+" ");
           }
           System.out.println();
           
           Connection conn=null;
           Statement sta=null;
          try {
              Class.forName("com.mysql.jdbc.Driver"); 
              String url = "jdbc:mysql://172.28.22.100:3306/2017jsj2?characterEncoding=utf-8";
                String dbusername = "2017jsj2";
                String dbpassword = "123abc";
                conn = DriverManager.getConnection(url, dbusername, dbpassword);
                sta=conn.createStatement();
                String a = " " + sum;
                String b=" "+answer;
//                String sql="insert into user201705050138 (name,pwd) values (111,111ad)";
                String sql="insert into user201705050215 (name,pwd) values('"+kkk+"','"+a+"') ";
                int row=sta.executeUpdate(sql);
                System.out.println(row);
                
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
          } catch (SQLException ex) {
              Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
          }finally{
              if(sta!=null){
                  try {
                      sta.close();
                  } catch (SQLException ex) {
                      Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
              if(conn!=null){
                  try {
                      conn.close();
                  } catch (SQLException ex) {
                      Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          }
               
       } catch (IOException ex) {
           Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
       }
       

   }
}